<?php
class ControllerAccountEdit extends AccountController{

	public function index() {
		M('account/customer');
		
		if ($this->request->isPost() && $this->validate()) {
			$this->model_account_customer->editCustomer($this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(US('account/account'));
		}
		
		$this->data['breadcrumbs'][] = array(
				'text'      => $this->_['text_edit'],
				'href'      => US('account/edit'),
				'separator' => $this->_['text_separator']
		);
		
		$this->document->addScript('static/javascript/jquery/jquery.validate.js');
			
		if (!$this->request->isPost()) {
			$this->vo->data = $this->model_account_customer->getCustomer($this->customer->getId());
		}

		$this->display('account/edit.tpl');
	}

	protected function validate() {
		if (($this->customer->getEmail() != $this->request->post['email']) 
				&& $this->model_account_customer->getTotalCustomersByEmail($this->request->post['email'])) {
			$this->setMessage('error_warning', $this->_['error_exists']);
			return false;
		}
		
		$this->load->library('form_validation', true);
		$this->form_validation->set_rules('firstname', '', 'required|range_length[1,32]', $this->_['error_firstname']);
		$this->form_validation->set_rules('lastname', '', 'required|range_length[1,32]', $this->_['error_lastname']);
		$this->form_validation->set_rules('email', '', 'required|range_length[5,96]|email', $this->_['error_email']);
		$this->form_validation->set_rules('telephone', '', 'required|range_length[3,32]', $this->_['error_telephone']);

		return $this->form_validation->run();
	}
}
?>